import os
import platform
import signal
import sys
import threading
import time
import uuid

import requests
from colorama import Fore, Style

import utils.static as static
from utils.logger import PluginLogger, handle_caught_exception
from utils.notifier import send_notification
from utils.tools import calculate_sha256, pause

logger = PluginLogger('CloudService')


def get_platform_info():
    system = platform.system().lower()
    machine = platform.machine().lower()

    if system == "windows":
        if machine == "amd64":
            return "windows x64"
        else:
            return f"windows {machine}"
    elif system == "linux":
        if machine == "x86_64":
            return "linux x64"
        else:
            return f"linux {machine}"
    elif system == "darwin":
        if machine == "x86_64" or machine == "arm64":
            return "mac x64" if machine == "x86_64" else "mac arm64"
        else:
            return f"mac {machine}"
    else:
        return f"{system} {machine}"


def get_user_uuid():
    app_dir = os.path.expanduser("~/.steamauto")
    uuid_file = os.path.join(app_dir, "uuid.txt")
    if not os.path.exists(app_dir):
        os.makedirs(app_dir)
    if not os.path.exists(uuid_file):
        with open(uuid_file, "w") as f:
            user_uuid = str(uuid.uuid4())
            f.write(user_uuid)
    else:
        with open(uuid_file, "r") as f:
            user_uuid = f.read().strip()
    return user_uuid


session = requests.Session()
session.headers.update({'User-Agent': f'Steamauto {static.CURRENT_VERSION} ({get_platform_info()}) {get_user_uuid()}'})


def compare_version(ver1, ver2):
    version1_parts = ver1.split(".")
    version2_parts = ver2.split(".")

    for i in range(max(len(version1_parts), len(version2_parts))):
        v1 = int(version1_parts[i]) if i < len(version1_parts) else 0
        v2 = int(version2_parts[i]) if i < len(version2_parts) else 0

        if v1 < v2:
            return -1
        elif v1 > v2:
            return 1

    return 0


def get_uu_uk_from_cloud():
    try:
        data = session.get('https://steamauto.jiajiaxd.com/tools/getUUuk').json()
        return data['uk']
    except Exception as e:
        handle_caught_exception(e, known=True)
        logger.warning('云服务异常，无法获取UK，将使用默认配置')
        return ''


def parseBroadcastMessage(message):
    message = message.replace('<red>', Fore.RED)
    message = message.replace('<green>', Fore.GREEN)
    message = message.replace('<yellow>', Fore.YELLOW)
    message = message.replace('<blue>', Fore.BLUE)
    message = message.replace('<magenta>', Fore.MAGENTA)
    message = message.replace('<cyan>', Fore.CYAN)
    message = message.replace('<white>', Fore.WHITE)
    message = message.replace('<reset>', Style.RESET_ALL)
    message = message.replace('<bold>', Style.BRIGHT)
    message = message.replace('<br>', '\n')
    return message


def autoUpdate(downloadUrl, sha256=''):
    import tqdm

    try:
        with session.get(downloadUrl, stream=True, timeout=30) as response:
            response.raise_for_status()

            # 获取文件名
            content_disposition = response.headers.get('Content-Disposition')
            if content_disposition and 'filename=' in content_disposition:
                filename = content_disposition.split('filename=')[1].strip('\"')
            else:
                # 如果没有 Content-Disposition 头部，从 URL 中提取文件名
                filename = downloadUrl.split('/')[-1]
                if not filename.endswith('.exe'):
                    filename += '.exe'  # 确保文件是可执行的

            total_size = int(response.headers.get('Content-Length', 0))
            downloaded_size = 0

            # 下载新版本的可执行文件
            with open(filename, 'wb') as file, tqdm.tqdm(
                desc=f'下载 {filename}',
                total=total_size,
                unit='B',
                unit_scale=True,
                unit_divisor=1024,
                miniters=1,
                dynamic_ncols=True,
            ) as bar:
                for data in response.iter_content(chunk_size=1024):
                    if not data:
                        break
                    file.write(data)
                    downloaded_size += len(data)
                    bar.update(len(data))
            logger.info('下载完成: %s', filename)
    except Exception as e:
        logger.error('下载失败')
        return False

    if sha256:
        logger.info('正在校验文件...')
        if calculate_sha256(filename) != sha256:
            logger.error('文件校验失败，将不会更新')
            return False
        logger.info('文件校验通过')

    # 创建update.txt，写入旧版本的路径，以便更新后删除旧版本
    with open('update.txt', 'w') as f:
        f.write(sys.executable)
    os.startfile(filename)  # type: ignore
    os._exit(0)
    sys.exit(0)
    pid = os.getpid()
    os.kill(pid, signal.SIGTERM)

    return True


def getAds():
    # 禁用广告获取功能，直接返回空列表
    return []


def checkVersion():
    # 禁用版本检查功能，直接返回 True
    static.is_latest_version = True
    return True


def adsThread():
    while True:
        time.sleep(600)
        getAds()


def versionThread():
    while True:
        time.sleep(43200)
        checkVersion()


ad = threading.Thread(target=adsThread)
update = threading.Thread(target=versionThread)
ad.start()
update.start()
